using KeGie.Admin.Server.Language;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace KeGie.Admin.Web.Controllers;

/// <summary>
/// 无权限控制器，用于处理403错误
/// </summary>
[AllowAnonymous]
public class UnauthorizedController : Controller
{
    /// <summary>
    /// 显示无权限页面
    /// </summary>
    /// <returns>无权限视图</returns>
    [Route("Unauthorized")]
    public IActionResult Index()
    {
        // 设置HTTP状态码为403
        HttpContext.Response.StatusCode = 403;
        
        // 设置视图数据
        ViewBag.ErrorMessage = "没有权限";
        ViewBag.CurrentPath = Request.Headers.ContainsKey("Referer") ? Request.Headers["Referer"].ToString() : Request.Path.ToString();
        
        return View();
    }
    
    /// <summary>
    /// 处理特定状态码的无权限错误
    /// </summary>
    /// <param name="statusCode">状态码</param>
    /// <returns>无权限视图</returns>
    [Route("Unauthorized/{statusCode}")]
    public IActionResult StatusCodeHandler(int statusCode)
    {
        if (statusCode == 403)
        {
            HttpContext.Response.StatusCode = 403;
            ViewBag.ErrorMessage = "没有权限";
        }
        else
        {
            HttpContext.Response.StatusCode = statusCode;
            ViewBag.ErrorMessage = "未知错误";
        }
        
        ViewBag.CurrentPath = Request.Headers.ContainsKey("Referer") ? Request.Headers["Referer"].ToString() : Request.Path.ToString();
        
        return View("Index");
    }
}